/** 
ii
*/

import java.sql.*;
import java.io.*;

public class AccessProverDB3 {

	Connection cnn;
	Statement  stmt;
	ResultSet  res;

	String url = "jdbc:oracle:thin:@prover.cs.csubak.edu:1521:PROVERDB",
		 user= "CS342", passwd= "student2";

	public AccessProverDB3() {
	   try {
            // Class.forName(driverName);
		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		cnn = DriverManager.getConnection(url, user, passwd);
		stmt = cnn.createStatement();
	   } catch (SQLException e ) { e.printStackTrace(); System.exit(-1); }
	}

	public static void main(String argv[]) {
		AccessProverDB3 ex3 = new AccessProverDB3(); // get connected, and create stmt.
		char c = ' ';
		String sql;
		ex3.user =   ScreenIO.promptForString("User Name: ");
		ex3.passwd = ScreenIO.promptForString("Password : ");
		while ( true ) {
			sql = ScreenIO.promptForString("SQL> ", ScreenIO.left);
			//if ( sql == null || sql.equals("") ) continue;
			try { c = sql.charAt(0); } catch (StringIndexOutOfBoundsException e) { continue;}
			if ( c == 'q' || c == 'Q' ) System.exit(0);
			ex3.executeSQL(sql);
			ScreenIO.displayResultSet(ex3.res);
		}
	}

	void executeSQL(String sql) {
	   try {
		if ( sql.charAt(sql.length() -1) == ';' ) sql = sql.substring(0, sql.length() -1);
		res = stmt.executeQuery(sql);
	   } catch (SQLException e) { System.err.println("\nError occured in executing SQL: \n\t" + sql + "\n"); }
	}


	protected void finalize() {
	   try {
		stmt.close();
		res.close();
		cnn.close();
	   } catch (SQLException e) {}
	   try { super.finalize(); } catch (Throwable e) {}
	}
}
